package avicit.bdp.dcs.log.rest;

import avicit.bdp.common.service.service.BdpCommonService;
import avicit.bdp.dcs.log.service.LogService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @金航数码科技有限责任公司
 * @作者：liyb
 * @邮箱：liyb@avic-digital.com
 * @创建时间： 2020-08-05 10:04
 * @类说明：日志接口
 * @修改记录：
 */
@RestController
@RequestMapping("/api/bdp/dcs/log/LogRest")
@Api(tags = "日志接口")
public class LogRest {

	private static final Logger logger = LoggerFactory.getLogger(LogRest.class);

	@Resource
	private LogService logService;

	@Resource
	private BdpCommonService bdpCommonService;

	@GetMapping("/getPerformanceByTaskId")
	@ApiOperation(value = "查询迁移、同步性能")
	public ResponseMsg<Map<String, List<Object>>> getPerformanceByTaskId(
			@ApiParam(value = "任务id", name = "taskId", required = true) @RequestParam String taskId,
			@ApiParam(value = "开始时间", name = "startTime") @RequestParam(required = false) String startTime,
			@ApiParam(value = "结束时间", name = "endTime") @RequestParam(required = false) String endTime) {

		ResponseMsg<Map<String, List<Object>>> responseMsg = new ResponseMsg<>();
		try {
			responseMsg.setResponseBody(logService.getPerformanceByTaskId(taskId, startTime, endTime));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			responseMsg.setRetCode(String.valueOf(HttpStatus.SC_INTERNAL_SERVER_ERROR));
			responseMsg.setErrorDesc(e.getMessage());
			responseMsg.setMessage(e.getMessage());
		}
		return responseMsg;
	}

	@GetMapping("/getLogByTaskId")
	@ApiOperation(value = "查询日志")
	public ResponseMsg<String> getLogByTaskId(
			@ApiParam(value = "任务实例ID", name = "taskInstanceId") @RequestParam(required = false) String taskInstanceId,
			@ApiParam(value = "jobId", name = "jobId") @RequestParam(required = false) String jobId,
			@ApiParam(value = "跳过行数", name = "skipLineNum", required = true) @RequestParam Integer skipLineNum,
			@ApiParam(value = "查询的行数", name = "limit", required = true) @RequestParam Integer limit) throws Exception {

		ResponseMsg<String> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(bdpCommonService.showLog(taskInstanceId, jobId, skipLineNum, limit));
		return responseMsg;
	}

    @GetMapping("/getTableLogByJobId")
    @ApiOperation(value = "查询创建表日志")
    public ResponseMsg<String> getTableLogByJobId(@ApiParam(value = "jobId", name = "jobId") @RequestParam(required = false) String jobId) throws Exception{
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(logService.getTableLogByJobId(jobId));
        return responseMsg;
    }

    @GetMapping("/getTableLogByTaskId")
    @ApiOperation(value = "查询创建表日志")
    public ResponseMsg<String> getTableLogByTaskId(@ApiParam(value = "流程实例ID", name = "processInstanceId") @RequestParam(required = false) String processInstanceId,
                                                   @ApiParam(value = "任务实例ID", name = "taskInstanceId") @RequestParam(required = false) String taskInstanceId)
            throws Exception{
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        responseMsg.setResponseBody(logService.getTableLogByTaskId(processInstanceId, taskInstanceId));
        return responseMsg;
    }

}
